package cn.iocoder.yudao.module.crm.controller.admin.course;

import cn.iocoder.yudao.module.crm.controller.admin.course.vo.CoursePackagePageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.course.vo.CoursePackageRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.course.vo.CoursePackageRltSaveReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.course.vo.CoursePackageSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crm.dal.dataobject.coursepackage.CoursePackageDO;
import cn.iocoder.yudao.module.crm.service.coursepackage.CoursePackageService;

@Tag(name = "管理后台 - 课程套餐")
@RestController
@RequestMapping("/crm/course-package")
@Validated
public class CoursePackageController {

    @Resource
    private CoursePackageService coursePackageService;

    @PostMapping("/create")
    @Operation(summary = "创建课程套餐")
    @PreAuthorize("@ss.hasPermission('crm:course-package:create')")
    public CommonResult<Long> createCoursePackage(@Valid @RequestBody CoursePackageSaveReqVO createReqVO) {
        return success(coursePackageService.createCoursePackage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程套餐")
    @PreAuthorize("@ss.hasPermission('crm:course-package:update')")
    public CommonResult<Boolean> updateCoursePackage(@Valid @RequestBody CoursePackageSaveReqVO updateReqVO) {
        coursePackageService.updateCoursePackage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程套餐")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:course-package:delete')")
    public CommonResult<Boolean> deleteCoursePackage(@RequestParam("id") Long id) {
        coursePackageService.deleteCoursePackage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程套餐")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:course-package:query')")
    public CommonResult<CoursePackageRespVO> getCoursePackage(@RequestParam("id") Long id) {
        CoursePackageDO coursePackage = coursePackageService.getCoursePackage(id);
        return success(BeanUtils.toBean(coursePackage, CoursePackageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程套餐分页")
    @PreAuthorize("@ss.hasPermission('crm:course-package:query')")
    public CommonResult<PageResult<CoursePackageRespVO>> getCoursePackagePage(@Valid CoursePackagePageReqVO pageReqVO) {
        PageResult<CoursePackageDO> pageResult = coursePackageService.getCoursePackagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CoursePackageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程套餐 Excel")
    @PreAuthorize("@ss.hasPermission('crm:course-package:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCoursePackageExcel(@Valid CoursePackagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CoursePackageDO> list = coursePackageService.getCoursePackagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程套餐.xls", "数据", CoursePackageRespVO.class,
                        BeanUtils.toBean(list, CoursePackageRespVO.class));
    }


    @PostMapping("/add-course")
    @Operation(summary = "套餐关联课程-新增")
    @PreAuthorize("@ss.hasPermission('crm:course-package:update')")
    public CommonResult<Boolean> addCourseByCoursePackage(@Valid @RequestBody CoursePackageRltSaveReqVO createReqVO) {
        coursePackageService.addCourse(createReqVO);
        return success(true);
    }

    @PostMapping("/delete-course")
    @Operation(summary = "套餐关联课程-删除")
    @PreAuthorize("@ss.hasPermission('crm:course-package:update')")
    public CommonResult<Boolean> deleteCourseByCoursePackage(@Valid @RequestBody CoursePackageRltSaveReqVO createReqVO) {
        coursePackageService.deleteCourse(createReqVO);
        return success(true);
    }

}